<?php

/* @var $this \yii\web\View */

use yii\helpers\Url;
?>
<script>
    var adminModelStr = '<?=Yii::$app->params['viewPrefix']?>AdminModel';
    var adminAuthStr = '<?=Yii::$app->params['viewPrefix']?>AdminAuth';

    Vue.prototype.$local_set_user = function (user) {
        let sp = window.localStorage;
        sp[adminModelStr] = JSON.stringify(user)
    }

    Vue.prototype.$local_load_user = function () {
        let sp = window.localStorage;
        if(!sp[adminModelStr] || sp[adminModelStr] == 'undefined')
            return null;
        return JSON.parse(sp[adminModelStr]);
    }

    Vue.prototype.$logout = function () {
        const auth = JSON.parse(localStorage.getItem(adminAuthStr));

        if (auth != null) {
            let mydata = {
                refresh_token: auth.refresh_token,
                access_token: auth.access_token
            };

            http.post("/auth/logout", http.param(mydata)).then(function (response) {
                localStorage.removeItem(adminAuthStr)
                localStorage.removeItem(adminModelStr)
                window.location.href = "<?=Url::to(['site/login'])?>";
            })
        }else{
            window.location.href = "<?=Url::to(['site/login'])?>";
        }
    }

    function setAuth(obj){
        localStorage.setItem(adminAuthStr, JSON.stringify(obj));
    }

    function getAuth(){
        return JSON.parse(localStorage.getItem(adminAuthStr))
    }
</script>
